// Package service @author dingrui @since 2021/11/27
package service

import (
	"fmt"
	"strconv"
	"time"

	"github.com/bannirui/mini-prize/dal/dao"
	"github.com/bannirui/mini-prize/dal/model"
)

var (
	G_userDayService UserDayService
)

type UserDayService interface {
	GetAll(page, size int) []model.UserDay
	CountAll() int64
	Search(uid, day int) []model.UserDay
	Count(uid, day int) int
	Get(id int) *model.UserDay
	Update(user *model.UserDay, columns []string) error
	Create(user *model.UserDay) error
	GetUserToday(uid int) *model.UserDay
}

type userDayService struct {
	dao *dao.UserDayDao
}

func init() {
	G_userDayService = &userDayService{
		dao: dao.G_userDayDao,
	}
}

func (s *userDayService) GetAll(page, size int) []model.UserDay {
	return s.dao.PageAll(page, size)
}

func (s *userDayService) CountAll() int64 {
	return s.dao.CountAll()
}

func (s *userDayService) Search(uid, day int) []model.UserDay {
	return s.dao.ListByUidAndDay(uid, day)
}

func (s *userDayService) Count(uid, day int) int {
	return s.dao.GetCntByUidAndDay(uid, day)
}

func (s *userDayService) Get(id int) *model.UserDay {
	return s.dao.GetById(id)
}

func (s *userDayService) Update(data *model.UserDay, columns []string) error {
	return s.dao.Update(data, columns)
}

func (s *userDayService) Create(data *model.UserDay) error {
	return s.dao.InsertOne(data)
}

func (s *userDayService) GetUserToday(uid int) *model.UserDay {
	y, m, d := time.Now().Date()
	strDay := fmt.Sprintf("%d%02d%02d", y, m, d)
	day, _ := strconv.Atoi(strDay)
	list := s.dao.ListByUidAndDay(uid, day)
	if list != nil && len(list) > 0 {
		return &list[0]
	} else {
		return nil
	}
}
